﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CodeGenerate
{
    public  class TypeUtil
    {
        /// <summary>
        /// Returns the SqlDbType for a give DbType
        /// </summary>
        /// <param name="dbType">The DbType to evaluate</param>
        /// <returns></returns>
        public static SqlDbType GetSqlDBType(DbType dbType)
        {
            switch (dbType)
            {
                case DbType.AnsiString:
                    return SqlDbType.VarChar;
                case DbType.AnsiStringFixedLength:
                    return SqlDbType.Char;
                case DbType.Binary:
                    return SqlDbType.VarBinary;
                case DbType.Boolean:
                    return SqlDbType.Bit;
                case DbType.Byte:
                    return SqlDbType.TinyInt;
                case DbType.Currency:
                    return SqlDbType.Money;
                case DbType.Date:
                    return SqlDbType.DateTime;
                case DbType.DateTime:
                    return SqlDbType.DateTime;
                case DbType.Decimal:
                    return SqlDbType.Decimal;
                case DbType.Double:
                    return SqlDbType.Float;
                case DbType.Guid:
                    return SqlDbType.UniqueIdentifier;
                case DbType.Int16:
                    return SqlDbType.Int;
                case DbType.Int32:
                    return SqlDbType.Int;
                case DbType.Int64:
                    return SqlDbType.BigInt;
                case DbType.Object:
                    return SqlDbType.Variant;
                case DbType.SByte:
                    return SqlDbType.TinyInt;
                case DbType.Single:
                    return SqlDbType.Real;
                case DbType.String:
                    return SqlDbType.NVarChar;
                case DbType.StringFixedLength:
                    return SqlDbType.NChar;
                case DbType.Time:
                    return SqlDbType.DateTime;
                case DbType.UInt16:
                    return SqlDbType.Int;
                case DbType.UInt32:
                    return SqlDbType.Int;
                case DbType.UInt64:
                    return SqlDbType.BigInt;
                case DbType.VarNumeric:
                    return SqlDbType.Decimal;

                default:
                    return SqlDbType.VarChar;
            }
        }

        /// <summary>
        /// Gets the type of the db.
        /// </summary>
        /// <param name="sqlType">Type of the SQL.</param>
        /// <returns></returns>
        public static DbType GetDbType(string sqlType)
        {
            switch (sqlType)
            {
                case "varchar":
                    return DbType.String;
                case "nvarchar":
                    return DbType.String;
                case "int":
                    return DbType.Int32;
                case "uniqueidentifier":
                    return DbType.Guid;
                case "datetime":
                    return DbType.DateTime;
                case "bigint":
                    return DbType.Int64;
                case "binary":
                    return DbType.Binary;
                case "bit":
                    return DbType.Boolean;
                case "char":
                    return DbType.AnsiStringFixedLength;
                case "decimal":
                    return DbType.Decimal;
                case "float":
                    return DbType.Double;
                case "image":
                    return DbType.Binary;
                case "money":
                    return DbType.Currency;
                case "nchar":
                    return DbType.String;
                case "ntext":
                    return DbType.String;
                case "numeric":
                    return DbType.Decimal;
                case "real":
                    return DbType.Single;
                case "smalldatetime":
                    return DbType.DateTime;
                case "smallint":
                    return DbType.Int16;
                case "smallmoney":
                    return DbType.Currency;
                case "sql_variant":
                    return DbType.String;
                case "sysname":
                    return DbType.String;
                case "text":
                    return DbType.String;
                case "timestamp":
                    return DbType.Binary;
                case "tinyint":
                    return DbType.Byte;
                case "varbinary":
                    return DbType.Binary;
                case "int identity":
                    return DbType.Int32;
                default:
                    return DbType.String;
            }
        }

        public static string GetSystemType(DbType dbType)
        {
            switch (dbType)
            {
                case DbType.AnsiString:
                    return "System.String";
                case DbType.AnsiStringFixedLength:
                    return "System.String";
                case DbType.Binary:
                    return "System.Byte[]";
                case DbType.Boolean:
                    return "System.Boolean";
                case DbType.Byte:
                    return "System.Byte";
                case DbType.Currency:
                    return "System.Decimal";
                case DbType.Date:
                    return "System.DateTime";
                case DbType.DateTime:
                    return "System.DateTime";
                case DbType.Decimal:
                    return "System.Decimal";
                case DbType.Double:
                    return "System.Double";
                case DbType.Guid:
                    return "System.Guid";
                case DbType.Int16:
                    return "System.Int16";
                case DbType.Int32:
                    return "System.Int32";
                case DbType.Int64:
                    return "System.Int64";
                case DbType.Object:
                    return "System.Object";
                case DbType.SByte:
                    return "System.SByte";
                case DbType.Single:
                    return "System.Single";
                case DbType.String:
                    return "System.String";
                case DbType.StringFixedLength:
                    return "System.String";
                case DbType.Time:
                    return "System.TimeSpan";
                case DbType.UInt16:
                    return "System.UInt16";
                case DbType.UInt32:
                    return "System.UInt32";
                case DbType.UInt64:
                    return "System.UInt64";
                case DbType.VarNumeric:
                    return "System.Decimal";
                default:
                    return "System.String";
            }
        }
    }
}
